﻿using Microsoft.Extensions.DependencyInjection;
using StackExchange.Redis;
using Volo.Abp.Modularity;

namespace Colorful.Caching;

/// <summary>
///  缓存模块
/// </summary>
public class ColorfulCachingModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        var configuration = context.Services.GetConfiguration();

        var redisEnabled = configuration["Redis:IsEnabled"];
        if (redisEnabled.IsNullOrEmpty() || bool.Parse(redisEnabled))
        {
            var connection = ConnectionMultiplexer.Connect(configuration["Redis:Configuration"]);

            context.Services.AddSingleton<IConnectionMultiplexer>(_ => connection);

            context.Services.AddTransient(_ => connection.GetDatabase());

            context.Services.AddTransient<IRedisCache, StackExchangeRedisCache>();
        }
    }
}